package com.gu.controller;

import Utils.JwtUtils;
import com.boot.exception.DiyException;
import com.gu.entity.UcenterMember;
import com.gu.entity.vo.LoginVo;
import com.gu.entity.vo.RegisterVo;
import com.gu.service.UcenterMemberService;
import entity.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: 用户中心控件
 *
 * @Autor: Rolin
 * datetime: 06-09-10:13
 */
@RestController
@CrossOrigin
@RequestMapping("/edu/ucenter")
@Api(tags = "用户信息管理")
public class UserCenterController {

    @Resource
    private UcenterMemberService ucenterMemberService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate ;

    @ApiOperation("登陆管理")
    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginVo loginVo){
        //单点登陆之后，返回token值：
        String token = ucenterMemberService.login(loginVo);
        System.out.println(token);
        return !StringUtils.isEmpty(token) ? CommonResult.success().data("token",token): CommonResult.error();
    }

    @ApiOperation("注册管理")
    @PostMapping("/register")
    public CommonResult register(@RequestBody RegisterVo registerVo){
        //注册需要短信验证功能
       boolean result = ucenterMemberService.register(registerVo);
       return result ? CommonResult.success() :CommonResult.error() ;
    }

    //此处还需要一个根据token来获取用户登陆信息
    @ApiOperation("用token来获取用户信息")
    @GetMapping("/getLoginInfo")
    public CommonResult getLoginInfo(HttpServletRequest request){
        UcenterMember  ucenterMember = ucenterMemberService.getLoginInfoByToken(request);
        return !StringUtils.isEmpty( ucenterMember.getMobile())? CommonResult.success().data("userInfo", ucenterMember):CommonResult.error();
    }

    /**
     * 订单远程调用专用接口
     * */
    @ApiOperation("订单接口远程调用获取userInfo")
    @GetMapping("/getUserInfo/{id}")
    public Map<String,Object> getUserInfo(@PathVariable("id")String member_id){

        UcenterMember member = ucenterMemberService.getById(member_id);
        String mobile = member.getMobile();
        String nickname = member.getNickname();

        Map<String, Object> map = new HashMap<>();
        map.put("member_id",member_id);
        map.put("mobile",mobile);
        map.put("nickname",nickname);

        return map;
    }

    @ApiOperation("显示日注册人数")
    @GetMapping("/getRegisterNum/{dateTime}")
    public CommonResult getRegisterNum(@PathVariable("dateTime")String dateTime){
       Integer nums  =  ucenterMemberService.getReNum(dateTime);
       return nums>0 ? CommonResult.success().data("num",nums): CommonResult.error() ;
    }
}
